package com.cuit.jifver.weibo.activity;

import java.util.HashMap;
import java.util.Map;

import com.cuit.jifver.weibo.R;
import com.cuit.jifver.weibo.data.OAuth;
import com.cuit.jifver.weibo.logic.MainService;
import com.cuit.jifver.weibo.logic.Task;
import com.cuit.jifver.weibo.utils.ConfigUtil;
import com.cuit.jifver.weibo.utils.Constants;
import com.cuit.jifver.weibo.utils.DialogUtil;
import com.cuit.jifver.weibo.utils.LogUtil;
import com.cuit.jifver.weibo.utils.TextUtil;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.graphics.Color;
import android.net.Uri;
import android.os.Bundle;
import android.widget.TextView;



/**
 * 展示AccessToken
 * 
 * 		1. 获取授权后成功后回调的URL并匹配出验证�? *  		（网易无验证码不处理�? *  	2.获取OAuth对象并设置验证码
 *  	3.获取accessToken
 *  
 * @author bywyu
 *
 */
public class ShowAccessTokenAct extends BaseActivity{
	
	private static int MODE = Context.MODE_WORLD_READABLE + Context.MODE_WORLD_WRITEABLE;
	@Override
    protected void onCreate(Bundle savedInstanceState) {
	    super.onCreate(savedInstanceState);
	    setContentView(R.layout.showaccesstoken_ui);
	    
	    String url = getIntent().getStringExtra(ConfigUtil.OAUTH_VERIFIER_URL);
    	Uri uri = Uri.parse(url);
    	//匹配验证�?		
    	String oauth_verifier = uri.getQueryParameter("oauth_verifier");
    	
    	Map map = new HashMap();
    	map.put(Constants.oauth.OAUTH_VERIFIER, oauth_verifier);
    	Task task = new Task(Constants.task.GET_ACCESS_TOKEN, map);
 	    MainService.addNewTask(task);
		showProgress("Please wait！");
		/*OAuth oAuth = OAuth.getInstance();
		oAuth.setOauthVerifier(oauth_verifier);
		String accessToken = oAuth.getAccessToken();	
		
		String oauth_token = oAuth.getOauth_token();
		String oauth_token_secret = oAuth.getOauth_token_secret();
		
	    TextView textView = (TextView)findViewById(R.id.accessTokenView);
	    textView.setTextColor(Color.RED);
	    textView.setTextSize(20);
		textView.setText( "oauth_verifier = "+oauth_verifier+" \noauth_token = "+oauth_token+"\noauth_token_secret = "+oauth_token_secret+"\naccessToken = "+accessToken);
		System.out.println("accessToken = "+accessToken);
		Map<String, String> map = TextUtil.splitResponse(accessToken);
		saveAccessToken(map.get(Constants.oauth.OAUTH_TOKEN), map.get(Constants.oauth.OAUTH_TOKEN_SECRET));*/
		
    }
	
	
	private void saveAccessToken(String accessToken, String accessSecret)
	{
		
		
		SharedPreferences preferences = getSharedPreferences(Constants.oauth.OAUTH_FILE_NAME, MODE);
		Editor editor = preferences.edit();
		editor.putString(ConfigUtil.access_token_name, accessToken);
		editor.putString(ConfigUtil.access_token_secret_name, accessSecret);
		editor.commit();
	}


	@Override
	public void refresh(Object... param)
	{
		
		int flag = ((Integer) param[0]).intValue();
		LogUtil.i(this,flag+"flag" );
		switch (flag)
		{
		
	   case Constants.task.GET_ACCESS_TOKEN:
		   dismissProgress();
	    	String resut = (String) param[1];
	    	if (resut == null || resut.equalsIgnoreCase("null") )
			{
				DialogUtil.OneButtonShowMessageDialog(this, "Request oauth_access_token fail!", Constants.task.GET_RUEST_TOKEN);
				return;
			}
	    	Intent intent = new Intent();
	    	intent.setClass(this, HomePageAct.class);
	    	startActivity(intent);
	    	ShowAccessTokenAct.this.finish();
	    	break;
		}
		
	}
	
}
